from django.db import models
from control.models.User import User
import uuid
from .Publications import Publication
from .Author import Author
from .Keyword import Keyword
from .GlobalTables import LiteratureStatusType, LiteratureType, Language

# 文献信息
class Literature(models.Model):
    uuid = models.UUIDField(primary_key = True,auto_created = True,default = uuid.uuid4,editable = False)
    title = models.CharField(verbose_name="引文名称", max_length=1024)         
    zh_title = models.CharField(verbose_name="引文中文名称", max_length=1024)    
    date = models.DateField(verbose_name="发表日期", null=True, blank=True)			
    abstract = models.TextField(verbose_name="摘要", null=True, blank=True)		
    language = models.ForeignKey(Language, verbose_name="语言", on_delete=models.CASCADE)
    DOI = models.CharField(verbose_name="DOI", max_length=256, null=True, blank=True)	
    status = models.ForeignKey(LiteratureStatusType, verbose_name="状态", on_delete=models.CASCADE)
    liter_code = models.CharField(verbose_name="引文编码", max_length=256, null=True, blank=True)
    comment = models.TextField(verbose_name="备注评论", null=True, blank=True)
    founder_info = models.TextField(verbose_name="资助方信息", null=True, blank=True)	# 名称列表，暂定先用文本保存而不单列每项，实际是分号隔开的list
    
    net_src = models.URLField(verbose_name="网络链接", max_length=1024, null=True, blank=True)
    db_src = models.CharField(verbose_name="数据库存储路径", max_length=256, null=True, blank=True)		
    net_src_state = models.BooleanField(verbose_name="网络链接有效性", default=True)	
    
    liter_type = models.ForeignKey(LiteratureType, verbose_name="引文类型", on_delete=models.CASCADE)
    pub_type = models.CharField(verbose_name="来源类型", max_length=256, null=True, blank=True)
    pub_id = models.ForeignKey(Publication, on_delete=models.CASCADE, null=True, verbose_name="来源出版物名称")
    
    volume = models.CharField(verbose_name="卷号", max_length=256, null=True, blank=True)	
    issue = models.CharField(verbose_name="期号", max_length=256, null=True, blank=True)		
    first_page = models.CharField(verbose_name="起始页码",null=True, blank=True, max_length=50)
    last_page = models.CharField(verbose_name="结束页码",null=True, blank=True, max_length=50)
    
    edition_number = models.CharField(verbose_name="版次", max_length=256, null=True, blank=True)
    cited_count = models.IntegerField(verbose_name="被引用数", default=0)

    cit_name = models.CharField(verbose_name="来源数据库名称",max_length=128, null=True, blank=True) 
    cit_id = models.CharField(verbose_name="来源库中编号", max_length=256, null=True, blank=True)	
    
    is_reviewing = models.BooleanField(verbose_name="审核状态", default=False)
    creator = models.ForeignKey(User, verbose_name="创建者", related_name="liter_creator", on_delete=models.CASCADE)				
    create_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    last_editor = models.ForeignKey(User, verbose_name="最后编辑者", related_name="liter_last_editor", on_delete=models.CASCADE)	
    last_edit_date = models.DateTimeField(verbose_name='最后编辑时间', auto_now=True)					
    reviewer = models.ForeignKey(User, verbose_name="审核者", related_name="liter_reviewer", on_delete=models.CASCADE, null=True)
    review_date = models.DateTimeField(null=True, blank=True, verbose_name='审核时间')
    					
    authors = models.ManyToManyField(Author, verbose_name="作者列表")	
    cited_list = models.ManyToManyField("self", symmetrical=False, verbose_name="引用文献")
    keywords = models.ManyToManyField(Keyword, verbose_name="关键词列表")		
    extra_properties = models.JSONField(
        verbose_name='额外属性', null=False, blank=True, default=dict)
    # components = models.ManyToManyField("Component")					# 文献中包含的所有组分

    class Meta:
        ordering = ['-title', '-zh_title', '-date', '-language', '-liter_type']
        verbose_name = "Literature"           # 单数名称
        verbose_name_plural = "Literatures"   # 复数名称
        indexes = [
            models.Index(fields=['create_date']),  # 倒序索引
            models.Index(fields=['uuid', 'create_date']),  # 覆盖索引
            models.Index(fields=['title', 'zh_title'], name='final_query_idx_literature')
        ]

    def __str__(self):
        return self.title + ': ' + self.zh_title